const { expect, use } = require('chai');
const { BigNumber } = require('ethers');
const { deployContract, MockProvider, solidity } = require('ethereum-waffle');
const { ethers, upgrades } = require("hardhat");

use(solidity);

describe("Test NFTSwap.buy_ERC1155 Interface", function () {
    var ERC20;
    var ERC721;
    var ERC1155;
    var OWNER;
    var ADDR1;
    var NFTSWAPCONTRACT;
    beforeEach(async () => {
        [OWNER, ADDR1] = await ethers.getSigners();

        const ERC20PresetMinterPauser = await ethers.getContractFactory("ERC20PresetMinterPauser", OWNER);
        ERC20 = await ERC20PresetMinterPauser.deploy("TestERC20", "T20");

        const ERC721PresetMinterPauserAutoId = await ethers.getContractFactory("ERC721PresetMinterPauserAutoId", OWNER);
        ERC721 = await ERC721PresetMinterPauserAutoId.deploy("TestERC721", "T721", "https://t721.com");

        const ERC1155PresetMinterPauser = await ethers.getContractFactory("ERC1155PresetMinterPauser", OWNER);
        ERC1155 = await ERC1155PresetMinterPauser.deploy("https://t1155.com");


        const NFTSwap = await ethers.getContractFactory("NFTSwap", OWNER);
        NFTSWAPCONTRACT = await upgrades.deployProxy(NFTSwap, ["0x8006C00F6217dE69f7F6630EEc377819CE68e37f", 1000000, 0, ERC20.address], {
            initializer: '__NFTSwap_init'
        });
        await NFTSWAPCONTRACT.deployed();

        // 实例化ERC1155 合约
        await ERC1155.deployed();

        // 增发 id=0 的token，并approve 给 NFTSwap
        var mintERC1155Tx0 = await ERC1155.connect(OWNER).mint(OWNER.address, 1, 10, "0x");
        await mintERC1155Tx0.wait();

        // 增发 id=0 的token，并approve 给 NFTSwap
        var mintERC1155Tx1 = await ERC1155.connect(OWNER).mint(OWNER.address, 2, 10, "0x");
        await mintERC1155Tx1.wait();

        var approveERC1155Tx = await ERC1155.connect(OWNER).setApprovalForAll(NFTSWAPCONTRACT.address, true);
        await approveERC1155Tx.wait();

        // 定义assets, assetType.ERC721 = 1
        var assets1 = [{ Contract: ERC1155.address, TokenId: BigNumber.from(1), TokenValue: BigNumber.from(1), Type: 2 }];
        var assets2 = [{ Contract: ERC1155.address, TokenId: BigNumber.from(2), TokenValue: BigNumber.from(1), Type: 2 }];

        // 发起交易
        const sellTx1 = await NFTSWAPCONTRACT.sell(assets1, 1, ADDR1.address, BigNumber.from(2000000));
        await sellTx1.wait();
        // // 发起交易
        const sellTx2 = await NFTSWAPCONTRACT.sell(assets2, 2, ADDR1.address, BigNumber.from(2000000));
        await sellTx2.wait();

    });

    it("Should be only Buy ERC721 successfully", async function () {

        await ERC20.deployed();
        // 增发ERC20，并approve 给 NFTSwap
        var mintERC20TxO = await ERC20.connect(OWNER).mint(ADDR1.address, BigNumber.from(2000000));
        await mintERC20TxO.wait();
        var approveERC20Tx = await ERC20.connect(ADDR1).approve(NFTSWAPCONTRACT.address, BigNumber.from(2000000));
        await approveERC20Tx.wait();
        const buyNFTTx = await NFTSWAPCONTRACT.connect(ADDR1).packSwap([2], 1332);
        await buyNFTTx.wait();
        var receipt = await ethers.provider.getTransactionReceipt(buyNFTTx.hash);
        expect(receipt.status).to.equal(1);
    });

    it("Should be pack Buy ERC721 successfully", async function () {

        await ERC20.deployed();
        // 增发ERC20，并approve 给 NFTSwap
        var mintERC20TxO = await ERC20.connect(OWNER).mint(ADDR1.address, BigNumber.from(4000000));
        await mintERC20TxO.wait();
        var approveERC20Tx = await ERC20.connect(ADDR1).approve(NFTSWAPCONTRACT.address, BigNumber.from(4000000));
        await approveERC20Tx.wait();
        const buyNFTTx = await NFTSWAPCONTRACT.connect(ADDR1).packSwap([1,2], 1332);
        await buyNFTTx.wait();
        var receipt = await ethers.provider.getTransactionReceipt(buyNFTTx.hash);
        expect(receipt.status).to.equal(1);
    });

});